//
//  WmcCLLocationManager.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/7/21.
//

import CoreLocation
import RxRelay
import UIKit

// MARK: - [WmcCLLocationManager]

class WmcCLLocationManager: NSObject {
    // Public

    /// 单例实例化
    public static let shared: WmcCLLocationManager = {
        let manager = WmcCLLocationManager()
        return manager
    }()

    // Internal

    /// 方位角
    let azimuthAngleRelay: BehaviorRelay<Double> = .init(value: 0)

    lazy var clLocationManager: CLLocationManager = {
        let manager = CLLocationManager()
        manager.delegate = self
        return manager
    }()

    /// 配置
    func config() {
    }
    
    /// 开始方位角更新
    func startUpdatingHeading() {
        clLocationManager.startUpdatingHeading()
    }
    
    /// 停止方位角更新
    func stopUpdatingHeading() {
        clLocationManager.stopUpdatingHeading()
    }

}

// MARK: - CLLocationManagerDelegate

extension WmcCLLocationManager: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        let heading = newHeading.magneticHeading
        azimuthAngleRelay.accept(heading)
    }

    func locationManagerShouldDisplayHeadingCalibration(_ manager: CLLocationManager) -> Bool {
        return true
    }
}
